/**
 * Created by Zhongxu on 2017/6/26.
 */
/**
 * @author: ZhongXu
 * @date:2017-06-14
 */

$(document).ready(
    function() {
        var VAL = {
            NORMAL : "0000000",
            T_FIPINQBOX : "FIPINQBOX",
            T_FIPINQSHT : "FIPINQSHT",
            T_FBPLSTDAT : "FBPBISDAT",
            T_FDPAPLYWO : "FDPAPLYWO",
            T_FBPMDLDEF : "FBPMDLDEF",
            T_FSPGRDCHG : "FSPGRDCHG",
            // EVT_USER :$("#userId").text(),
            EVT_USER    :"C1706004",
            DISABLED_ATTR : {
                'disabled' : true
            },
            ENABLED_ATTR : {
                'disabled' : false
            }
        };
        var domObj = {
            $boxIdFk : $("#boxIdFk"),
            $prdIdFk :$("#prdIdFk"),
            $boxId : $("#boxId"),
            $boxStat : $("#boxStat"),
            $prdQty : $("#prdQty"),
            $chipQty : $("#chipQty"),
            $nxPathIdFk : $("#nxPathIdFk"),
            $nxPathVerFk : $("#nxPathVerFk"),
            $nxOpeNoFk : $("#nxOpeNoFk"),
            $acRecipeId : $("#acRecipeId"),
            $woIdFk : $("#woIdFk"),
            $toolIdFk : $("#toolIdFk"),
            buttons : {
                $query_btn : $("#query_btn"),
                $selectReason_btn : $("#selectReason_btn"),
                $deleteReason_btn : $("#deleteReason_btn"),
                $updateLev_btn : $("#updateLev_btn"),
                $commit_btn : $("#commit_btn"),
                $upload_btn : $("#upload_btn"),
                $select_file : $("#select_file"),
                $file_name : $("#file_name"),
                $download_btn : $("#download_btn")
            },
            grids : {
                $reasonListGrd : $("#reasonListGrd"),
                $reasonListDiv : $("#reasonListDiv"),
                $reasonListPg : $("#reasonListPg"),

                $glassListGrd : $("#glassListGrd"),
                $glassListDiv : $("#glassListDiv"),
                $glassListPg : $("#glassListPg")
            },
            dialogs : {
                $addReasonDialog : $("#addReasonDialog"),
                $updateLevDialog : $("#updateLevDialog"),
                $deptId : $("#deptId"),
                $prdLevel : $("#prdLevel"),
                $modalReasonListDiv : $("#modalReasonListDiv"),
                $modalReasonListGrd : $("#modalReasonListGrd"),
                $modalReasonListPg : $("#modalReasonListPg"),

                $reModalReasonListDiv : $("#reModalReasonListDiv"),
                $reModalReasonListGrd : $("#reModalReasonListGrd"),
                $reModalReasonListPg : $("#reModalReasonListPg"),

                // 对话框的确认按钮
                $reasonDialog_sureBtn : $("#reasonDialog_sureBtn"),
                $updateLevDialog_sureBtn : $("#updateLevDialog_sureBtn"),

            }

        }
        var lastrow, lastcell,lastId;
        var btnFunc = {
            queryBtnfunc : function() {
                clearDataAction();
                var boxIdFk, prdIdFk,iary, inObj, outObj;
                boxIdFk = domObj.$boxIdFk.val();// 卡匣id
                prdIdFk = domObj.$prdIdFk.val();// 玻璃id
                if ("" == boxIdFk&&""==prdIdFk) {
                    showErrorDialog("", "卡匣代码和玻璃代码不能都为空");
                    return false;
                }
                if(""!=prdIdFk){//查询卡匣里面某一块玻璃信息
                    inObj = {
                        trx_id : VAL.T_FIPINQSHT,
                        action_flg : "T",
                        prd_seq_id : prdIdFk
                    };
                    outObj = comTrxSubSendPostJson(inObj);
                    if (outObj.rtn_code === VAL.NORMAL) {
                        if(outObj.prd_tbl==undefined){
                            showErrorDialog("", "当前玻璃代码有误");
                            return false;
                        }
                        var boxId = domObj.$boxIdFk.val();// 卡匣id
                        if(""!=boxId&&boxId!=outObj.prd_tbl.box_id_fk){
                            showErrorDialog("", "玻璃不在当前卡匣内");
                            return false;
                        }
                        setGridInfo(outObj.prd_tbl, "#glassListGrd");
                        if(outObj.prd_tbl!=null){
                            var boxIdFk,iarys,inObjs,outObjs;
                            boxIdFk=outObj.prd_tbl.box_id_fk;
                            iarys = {
                                box_id : boxIdFk
                            };
                            inObjs = {
                                trx_id : VAL.T_FIPINQBOX,
                                action_flg : "Q",
                                box_id : boxIdFk,
                                iary : [iarys]
                            };
                            outObjs = comTrxSubSendPostJson(inObjs);
                            if (outObjs.rtn_code === VAL.NORMAL) {
                                var chip_qty;//计算面板数量
                                chip_qty=(outObjs.prd_qty)*(outObjs.oary[0].prd_qty);
                                domObj.$boxId.val(outObjs.box_id);
                                domObj.$boxStat.val(outObjs.box_stat);
                                domObj.$prdQty.val(outObjs.prd_qty);
                                domObj.$chipQty.val(chip_qty);
                                domObj.$nxPathIdFk.val(outObjs.nx_path_id_fk);
                                domObj.$nxPathVerFk.val(outObjs.nx_path_ver_fk);
                                domObj.$nxOpeNoFk.val(outObjs.nx_ope_no_fk);
                                domObj.$acRecipeId.val(outObjs.oary[0].ac_recipe_id);
                                domObj.$woIdFk.val(outObj.wo_id_fk);
                                domObj.$toolIdFk.val(outObjs.tool_id_fk);
                            }
                        }
                    }
                }
                if(""!= boxIdFk&&""==prdIdFk){//查询箱子里面所有的玻璃信息

                    iary = {
                        box_id : boxIdFk
                    };
                    inObj = {
                        trx_id : VAL.T_FIPINQBOX,
                        action_flg : "Q",
                        box_id : boxIdFk,
                        iary : [iary]
                    };
                    outObj = comTrxSubSendPostJson(inObj);
                    if (outObj.rtn_code === VAL.NORMAL) {
                        // 初始化卡匣信息

                        domObj.$boxId.val(outObj.box_id);
                        domObj.$boxStat.val(outObj.box_stat);
                        domObj.$prdQty.val(outObj.prd_qty);
                        domObj.$chipQty.val( chip_qty);
                        domObj.$nxPathIdFk.val(outObj.nx_path_id_fk);
                        domObj.$nxPathVerFk.val(outObj.nx_path_ver_fk);
                        domObj.$nxOpeNoFk.val(outObj.nx_ope_no_fk);

                        domObj.$woIdFk.val(outObj.wo_id_fk);
                        var data = new Array();
                        if(outObj.oary != undefined && outObj.oary.length>0){
                            domObj.$acRecipeId
                                .val(outObj.oary[0].ac_recipe_id);
                            chip_qty=(outObj.prd_qty)*(outObj.oary[0].prd_qty);
                            for(var i=0;i<outObj.oary.length;i++){
                                data[i]=outObj.oary[i];
                                data[i].cus_id_fk=outObj.cus_id_fk;

                            }
                            // 初始化玻璃信息
                            setGridInfo(data, "#glassListGrd");
                        }
                    }

                }
            },
            selectReasonBtnfunc : function() {
                domObj.dialogs.$modalReasonListGrd
                    .jqGrid("clearGridData");
                domObj.dialogs.$reModalReasonListGrd
                    .jqGrid("clearGridData");
                // 原因代码对话框的模型
                domObj.dialogs.$addReasonDialog.modal({
                    backdrop : true,
                    keyboard : false,
                    show : false
                });
                domObj.dialogs.$addReasonDialog.modal("show");

                //初始化部门代码
                comAddValueByDataCateFnc("#deptId","DEPT","data_ext","data_ext",true);
                var deptId, iary, inObj, outObj;
                deptId = domObj.dialogs.$deptId.val();
                iary = {
                    data_cate : "GDRS",
                };
                inObj = {
                    trx_id : VAL.T_FBPLSTDAT,
                    action_flg : "Q",
                    iary : [ iary ]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    var deptIds = domObj.dialogs.$deptId.val();
                    var oary = [];
                    for (var i = 0; i < outObj.oary.length; i++) {
                        var reason = {};
                        reason.deptId = deptIds;
                        reason.data_id = outObj.oary[i].data_id;
                        reason.data_desc = outObj.oary[i].data_desc;
                        oary.push(reason);
                    }
                    setGridInfo(oary,"#modalReasonListGrd");
                }
            },
            deleteReasonBtnfunc : function() {
                var selRowId = domObj.grids.$reasonListGrd.jqGrid(
                    "getGridParam", "selrow");
                if (selRowId==null||selRowId.length < 1) {
                    showErrorDialog("", "请至少选择一条原因代码进行删除");
                    return false;
                }
                $("#deleteReason_btn").showCallBackWarnningDialog(
                    {
                        errMsg : "是否删选原因代码,请确认!!!!",
                        callbackFn : function(data) {
                            if (data.result == true) {
                                // 动态删除jqgrid行时，数据会在动态变化
                                var length = selRowId.length;
                                for (var i = 0; i < length; i++) {
                                    domObj.grids.$reasonListGrd.jqGrid("delRowData",selRowId[0]);
                                }
                            }
                        }
                    });
            },
            updateLevBtnfunc : function() {
                var rowId= domObj.grids.$glassListGrd
                    .jqGrid("getGridParam", "selrow");
                if(rowId==null||rowId.length < 1){
                    showErrorDialog("", "请至少选择一条玻璃信息进行修改");
                    return false;
                }
                rowData = domObj.grids.$glassListGrd.jqGrid(
                    "getRowData", rowId);
                var grade=rowData.chip_grade;//获取小板的等级
                var grades =grade.split("");
                var infoCM=[];
                var x = rowData.x_axis_cnt_fk;
                var y =  rowData.y_axis_cnt_fk;
                if( x == "" ){
                    showErrorDialog("0003","X Count为空，请输入!");
                    return false;
                }
                if( y == "" ){
                    showErrorDialog("0003","Y Count为空，请输入!");
                    return false;
                }
                for(var i=1;i<=x;i++){
                    var str_pos={};
                    str_pos = {name:"pos_id_"+i  ,index:"pos_id_"+i   ,label : i+"Location",width :"80px" ,align:"left" ,sortable:false,editable:true};
                    infoCM.push(str_pos);
                }
                setLayoutGridInfo(infoCM);
                var a = new Array();
                for(var j=1;j<=y;j++){
                    a[j-1] = [];
                    for(var i=1;i<=x;i++){
                        if(grade[(j-1)*x+i-1]!=undefined){
                            a[j-1][i-1]= grades[(j-1)*x+i-1];
                        }else{
                            a[j-1][i-1]="";
                        }
                    }
                }
                for(var j=1;j<=y;j++){
                    var str="";
                    for(var i=1;i<=x;i++){
                        str +="\"pos_id_"+i+"\":\""+a[j-1][i-1]+"\"";
                        if( i != x ){
                            str += ",";
                        }
                    }
                    var ss = "{" + str + "}";
                    var rowData = eval("("+ss+")");
                    $("#layoutGrd").jqGrid("addRowData", j, rowData);
                }
                domObj.dialogs.$updateLevDialog.modal({
                    backdrop : true,
                    keyboard : false,
                    show : false
                });
                domObj.dialogs.$updateLevDialog.modal("show");
            },
            commitFnc: function() {
                var reasonRowData =domObj.grids.$reasonListGrd.jqGrid("getRowData");
                var Data = domObj.grids.$glassListGrd.jqGrid("getRowData");
                if(reasonRowData.length<1){
                    showErrorDialog("", "原因代码不能为空");
                    return;
                }
                if(Data.length<1){
                    showErrorDialog("", "玻璃信息表不能为空");
                    return;
                }
                boxId = domObj.$boxIdFk.val();
                rsnCode = reasonRowData[0].deptId;
                rsnCate = "GDRS";
                rsnDept = reasonRowData[0].data_id;
                iary = {
                    rsn_code       : rsnCode,
                    rsn_cate       : rsnCate,
                    rsn_dept       : rsnDept,
                    evt_usr        : VAL.EVT_USER ,
                    ret_prd_info   : Data
                };
                inObj = {
                    trx_id : VAL.T_FSPGRDCHG,
                    action_flg : "U",
                    iary : [iary]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    showSuccessDialog("等级修改成功");
                    btnFunc.queryBtnfunc();//更新信息
                }
            },
            uploadFnc: function(obj) {
                /*
                 FileReader共有4种读取方法：
                 1.readAsArrayBuffer(file)：将文件读取为ArrayBuffer。
                 2.readAsBinaryString(file)：将文件读取为二进制字符串
                 3.readAsDataURL(file)：将文件读取为Data URL
                 4.readAsText(file, [encoding])：将文件读取为文本，encoding缺省值为'UTF-8'
                 */
                var wb;//读取完成的数据
                var rABS = false; //是否将文件读取为二进制字符串
                if(!obj.files) {
                    return;
                }
                var fileName = obj.value;
                if(fileName.length > 1 && fileName ) {
                    var ldot = fileName.lastIndexOf(".");
                    var type = fileName.substring(ldot + 1);
                    if(!(type=="xlsx"||type=="xls")) {
                        showErrorDialog("", "只能上传excel文件");
                        return;
                    }
                    // var length = fileName.lastIndexOf("\\");
                    // var name = fileName.substring(length + 1);
                    // if(name!=null) {
                    //     domObj.buttons.$file_name.val(name);
                    // }

                }
                var f = obj.files[0];
                var reader = new FileReader();
                reader.onload = function(e) {
                    var data = e.target.result;
                    if(rABS) {
                        wb = XLSX.read(btoa(fixdata(data)), {//手动转化
                            type: 'base64'
                        });
                    } else {
                        wb = XLSX.read(data, {
                            type: 'binary'
                        });
                    }
                    //wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
                    //wb.Sheets[Sheet名]获取第一个Sheet的数据
                    var data = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
                    var Data= [];
                    var inObj,outObj;
                    for(var i=0;i<data.length;i++){
                        inObj = {
                            trx_id : VAL.T_FIPINQSHT,
                            action_flg : "Q",
                            prd_seq_id : data[i].prd_seq_id
                        };
                        outObj = comTrxSubSendPostJson(inObj);
                        if (outObj.rtn_code === VAL.NORMAL) {
                            // Data[i]=outObj;
                            var dataObj = {
                                prd_seq_id : data[i].prd_seq_id,
                                chip_grade : data[i].chip_grade,
                                slot_no : outObj.slot_no,
                                prd_stat : outObj.prd_stat,
                                prd_qty : outObj.prd_qty,
                                box_id_fk : outObj.box_id_fk,
                                mdl_id_fk : outObj.mdl_id_fk,
                                prd_grade : outObj.prd_grade,
                                // chip_grade :outObj.chip_grade,
                                wo_id_fk : outObj.wo_id_fk,
                                x_axis_cnt_fk : outObj.x_axis_cnt_fk,
                                y_axis_cnt_fk : outObj.y_axis_cnt_fk,
                                cus_id_fk : outObj.cus_id
                             }

                            if(!outObj.cus_id){
                                showErrorDialog("","客户代码不能为空！")
                                return ;
                            }
                            if(!outObj.mdl_id_fk){
                                showErrorDialog("","产品代码不能为空！")
                                return;
                            }
                            var regex = new RegExp('X', 'g'); // 使用g表示整个字符串都要匹配
                            var result =data[i].chip_grade.match(regex);
                            var level = !result ? 0 : result.length;
                            var iary = {mdl_id : outObj.mdl_id_fk};
                            var inTrxObj = {
                                trx_id : VAL.T_FBPMDLDEF,
                                action_flg : "Q",
                                iary : [iary]
                            };
                            var outTrxObj = comTrxSubSendPostJson(inTrxObj);
                            if(outTrxObj.rtn_code === VAL.NORMAL){

                                if(outTrxObj.oary[0].oaryB.length<1){
                                    showErrorDialog("","该玻璃的等级规则不存在!");
                                    return false;
                                }
                                for (var j = 0; j < outTrxObj.oary[0].oaryB.length; j++) {
                                    if(level>=outTrxObj.oary[0].oaryB[j].low_ng_limt && level<=outTrxObj.oary[0].oaryB[j].up_ng_limt){
                                        dataObj.prd_grade=outTrxObj.oary[0].oaryB[j].prd_grade;
                                    }
                                }
                            }
                            Data.push(dataObj);
                        }
                    }
                    setGridInfo(Data, "#glassListGrd");
                    // document.getElementById("demo").innerHTML= JSON.stringify( XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]) );
                };
                if(rABS) {
                    reader.readAsArrayBuffer(f);
                } else {
                    reader.readAsBinaryString(f);
                }
                function fixdata(data) { //文件流转BinaryString
                    var o = "", l = 0, w = 10240;
                    for(; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
                    o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
                    return o;
                }
            },
            select_file: function() {
                document.getElementById("upload_btn").click();
            },
            downloadTempFun: function () {
                if ($("#downForm").length > 0) {
                    $("#downForm").remove();
                }
                var str = '<form id="downForm" action="download.do" method="post">';
                str = str + '<input type="hidden" name="fileName" id= "fileName" />';
                str = str + "</form>";

                $(str).appendTo("body");
                $("#fileName").val("2A00.xls");
                $("#downForm").submit();
            }
        }
        // 主页面grid初始化
        var iniGridInfo = function() {
            domObj.grids.$reasonListGrd.jqGrid({
                url : "",
                datatype : "local",
                mtype : "POST",
                height : 100,
                width : domObj.grids.$reasonListDiv.width(),
                autowidth : true,
                shrinkToFit : true,
                resizable : true,
                loadonce : true,
                viewrecords : true,
                rownumbers : true,
                rowNum : 20,
                emptyrecords : true,
                multiselect : false,
                pager : domObj.grids.$reasonListPg,
                colModel : [ {
                    name : 'deptId',
                    index : 'deptId',
                    label : '部门代码',
                    width : 250
                }, {
                    name : 'data_id',
                    index : 'data_id',
                    label : '原因代码',
                    width : 250
                }, {
                    name : 'data_desc',
                    index : 'data_desc',
                    label : '原因代码说明',
                    width : 450
                },]
            });
            domObj.grids.$glassListGrd.jqGrid({
                url : "",
                datatype : "local",
                mtype : "POST",
                height : 350,
                width : domObj.grids.$glassListDiv.width(),
                autowidth : true,
                shrinkToFit : false,
                resizable : true,
                loadonce : true,
                viewrecords : true,
                rownumbers : true,
                rowNum : 20,
                rownumWidth : 30,
                emptyrecords : true,
                multiselect : false,
                pager : domObj.grids.$glassListPg,
                colModel : [ {
                    name : 'prd_seq_id',
                    index : 'prd_seq_id',
                    label : '玻璃代码',
                    width : 100
                }, {
                    name : 'slot_no',
                    index : 'slot_no',
                    label : '子格位',
                    width : 70
                }, {
                    name : 'prd_stat',
                    index : 'prd_stat',
                    label : '玻璃状态',
                    width : 100
                }, {
                    name : 'prd_qty',
                    index : 'prd_qty',
                    label : '面板数量',
                    width : 80
                }, {
                    name : 'box_id_fk',
                    index : 'box_id_fk',
                    label : '卡匣代码',
                    width : 150
                }, {
                    name : 'mdl_id_fk',
                    index : 'mdl_id_fk',
                    label : '产品代码',
                    width : 150
                },{
                    name : 'prd_grade',
                    index : 'prd_grade',
                    label : '大板等级',
                    width : 70
                }, {
                    name  : 'chip_grade',
                    index : 'chip_grade',
                    label : '小板等级',
                    width : 300
                },{
                    name : 'wo_id_fk',
                    index : 'wo_id_fk',
                    label : '工单信息',
                    width : 120
                },{
                    name : 'x_axis_cnt_fk',
                    index : 'x_axis_cnt_fk',
                    label : 'X轴数量',
                    width : 70
                },{
                    name : 'y_axis_cnt_fk',
                    index : 'y_axis_cnt_fk',
                    label : 'Y轴数量',
                    width : 70
                },{
                    name : 'cus_id_fk',
                    index : 'cus_id_fk',
                    label : '客户代码',
                    width : 150
                }
                ]
            });



        }
        // 重工代码dailog对话框grid初始化
        var inModelGrid = function() {
            domObj.dialogs.$modalReasonListGrd
                .jqGrid("clearGridData");
            domObj.dialogs.$reModalReasonListGrd
                .jqGrid("clearGridData");
            domObj.dialogs.$modalReasonListGrd
                .jqGrid({
                    url : "",
                    datatype : "local",
                    mtype : "POST",
                    height : 300,
                    // width :
                    // domObj.dialogs.$modalReasonListDiv.width()*0.99,
                    width : 400,
                    autowidth : true,
                    shrinkToFit : true,
                    resizable : true,
                    loadonce : true,
                    viewrecords : true,
                    rownumbers : true,
                    rowNum : 20,
                    emptyrecords : true,
                    multiselect : false,
                    pager : domObj.dialogs.$modalReasonListPg,
                    colModel : [ {
                        name : 'deptId',
                        index : 'deptId',
                        label : '部门代码',
                        hidden : true,
                        width : 100
                    }, {
                        name : 'data_id',
                        index : 'data_id',
                        label : '原因代码',
                        width : 150
                    }, {
                        name : 'data_desc',
                        index : 'data_desc',
                        label : '原因代码说明',
                        width : 180
                    } ],
                    onSelectRow : function(rowId, status) {
                        var deptId = domObj.dialogs.$deptId.val();
                        if(!deptId){
                            showErrorDialog("", "请选择部门代码");
                            return false;
                        }
                        var rowId= domObj.dialogs.$modalReasonListGrd
                            .jqGrid("getGridParam", "selrow");
                        var rowData = domObj.dialogs.$modalReasonListGrd.jqGrid(
                            "getRowData", rowId);
                        rowData.deptId = deptId;
                        setGridInfo(rowData, "#reModalReasonListGrd");
                    }
                });
            domObj.dialogs.$reModalReasonListGrd.jqGrid({
                url : "",
                datatype : "local",
                mtype : "POST",
                height : 400,
                // width:domObj.dialogs.$reModalReasonListDiv.width()*
                // 0.99,
                width : 400,
                autowidth : true,
                shrinkToFit : true,
                resizable : true,
                loadonce : true,
                viewrecords : true,
                rownumbers : true,
                rowNum : 20,
                emptyrecords : true,
                multiselect  : false,
                pager : domObj.dialogs.$reModalReasonListPg,
                colModel : [ {
                    name : 'deptId',
                    index : 'deptId',
                    label : '部门代码',
                    width : 100
                }, {
                    name : 'data_id',
                    index : 'data_id',
                    label : '原因代码',
                    width : 100
                }, {
                    name : 'data_desc',
                    index : 'data_desc',
                    label : '原因代码说明',
                    width : 100,
                } ]
            });

        }
        var dialogFnc = {
            reasonSureFnc : function() {
                var Data = domObj.dialogs.$reModalReasonListGrd.jqGrid("getRowData");
                if(Data.length<1){
                    showErrorDialog("", "请选择一条原因代码");
                    return false;
                }
                setGridInfo(Data, "#reasonListGrd");
                domObj.dialogs.$addReasonDialog.modal("hide");
            },
            updateLevSureFnc : function() {
                $("#layoutGrd").jqGrid("saveCell",lastrow,lastcell);//保存最后一次单元格的编辑状态
                var rowId= domObj.grids.$glassListGrd.jqGrid("getGridParam", "selrow");
                var rowData = domObj.grids.$glassListGrd.jqGrid(
                    "getRowData", rowId);
                // domObj.grids.$glassListGrd.jqGrid("delRowData",rowId);
                var x = rowData.x_axis_cnt_fk;
                var  dataId = rowData.cus_id_fk;
               /* if(dataId == ""){
                    showErrorDialog("0004","客户代码不能为空!");
                    return false;
                }*/
                var  mdlIdFk = rowData.mdl_id_fk;
                if(mdlIdFk == ""){
                    showErrorDialog("0004","产品代码不能为空!");
                    return false;
                }
                var grade="";
                var level=0;
                var data = $("#layoutGrd").jqGrid("getRowData");
                for(var i=0;i<data.length;i++){
                    for(var pos_id in data[i]){
                        if(data[i][pos_id]=='O'){
                            grade+=data[i][pos_id];
                        }else if(data[i][pos_id]=='X'){
                            grade+=data[i][pos_id];
                            level=level+1;
                        }
                    }
                }
                iary = {
                    mdl_id  : mdlIdFk
                };
                inObj = {
                    trx_id : VAL.T_FBPMDLDEF,
                    action_flg : "Q",
                    iary : [ iary ]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    if(outObj.oary[0].oaryB.length<1){
                        showErrorDialog("0004","该玻璃的等级规则不存在!");
                        return false;
                    }
                    for (var i = 0; i < outObj.oary[0].oaryB.length; i++) {
                        if(level>=outObj.oary[0].oaryB[i].low_ng_limt && level<=outObj.oary[0].oaryB[i].up_ng_limt){
                            rowData.prd_grade=outObj.oary[0].oaryB[i].prd_grade;
                        }
                    }
                }
                rowData.chip_grade = grade;
                domObj.grids.$glassListGrd.jqGrid("setRowData",rowId, rowData);
                domObj.dialogs.$updateLevDialog.modal("hide");
            }
        }
        //
        var setLayoutGridInfo = function(infoCM){
            $("#layoutDiv").empty();
            var str = '<table id="layoutGrd"></table><div id="layoutPg"></div>';
            $(str).appendTo($("#layoutDiv"));
            $("#layoutGrd").jqGrid({
                url:"",
                datatype:"local",
                mtype:"POST",
                height:270,
                width:850,
                //  autowidth:true,
                shrinkToFit:true,
                scroll:true,
                rownumWidth : true,
                resizable : true,
                rowNum:40,
                loadonce:true,
                viewrecords:true,
                sortable:false,
                rownumbers  :true ,//显示行号
                rownumWidth : 30,  //行号列宽度
                pager : "#layoutPg",
                fixed: true,
                cellEdit:true,
                colModel: infoCM,
                cellsubmit:'clientArray',
                beforeEditCell:function(rowid,cellname,v,iRow,iCol){
                    lastId  = rowid;
                    lastrow = iRow;
                    lastcell = iCol;
                }

            });
        };
        var iniButtonAction = function() {
            domObj.buttons.$query_btn.click(function() {
                btnFunc.queryBtnfunc();
            });
            domObj.buttons.$selectReason_btn.click(function() {
                btnFunc.selectReasonBtnfunc();
            });
            domObj.buttons.$deleteReason_btn.click(function() {
                btnFunc.deleteReasonBtnfunc();
            });
            domObj.buttons.$updateLev_btn.click(function() {
                btnFunc.updateLevBtnfunc();
            });
            domObj.buttons.$download_btn.click(function () {
                btnFunc.downloadTempFun();
            });
            domObj.buttons.$commit_btn.bind('click',
                btnFunc.commitFnc);
            domObj.buttons.$select_file.bind('click',
                btnFunc.select_file);
            domObj.buttons.$upload_btn.change(function() {
                btnFunc.uploadFnc(this);
            });
            // 确认按钮绑定click事件函数
            domObj.dialogs.$reasonDialog_sureBtn.bind('click',
                dialogFnc.reasonSureFnc);
            // 确认按钮绑定click事件函数
            domObj.dialogs.$updateLevDialog_sureBtn.bind('click',
                dialogFnc.updateLevSureFnc);
        }

        var clearDataAction = function() {
            domObj.$boxId.val("");
            domObj.$boxStat.val("");
            domObj.$prdQty.val("");
            domObj.$chipQty.val("");
            domObj.$nxPathIdFk.val("");
            domObj.$nxPathVerFk.val("");
            domObj.$nxOpeNoFk.val("");
            domObj.$acRecipeId.val("");
            domObj.$woIdFk.val("");
            $("#reasonListGrd").jqGrid('clearGridData');
            $("#glassListGrd").jqGrid('clearGridData');
            $("#modalReasonListDiv").jqGrid('clearGridData');
            $("#reModalReasonListDiv").jqGrid('clearGridData');
            $("#rePathListDiv").jqGrid('clearGridData');

        }
        var otherActionBind = function(){
            //Stop from auto commit
            $("form").submit(function(){
                return false;
            });

        };
        domObj.$boxIdFk.keydown(function(event) {
            if (event.which == 13) {
                btnFunc.queryBtnfunc();
            }
        });
        domObj.$prdIdFk.keydown(function(event) {
            if (event.which == 13) {
                btnFunc.queryBtnfunc();
            }
        });
        // function initFileInput(ctrlName, uploadUrl) {//初始化函数
        //     var control = $('#' + ctrlName);
        //     control.fileinput({
        //         language: 'zh', //设置语言
        //         uploadUrl: uploadUrl, //上传的地址
        //         uploadAsync:true,
        //         allowedFileExtensions : ['jpg', 'png','gif','xlsx'],//接收的文件后缀
        //         showUpload: false, //是否显示上传按钮
        //         showCaption: false,//是否显示标题
        //         showRemove:false,//是否显示删除按钮
        //         showPreview:false,//预览
        //         showCaption:false,
        //        // showBrowse:false,
        //         dropZoneEnabled:false,//拖拽区域
        //         browseClass: "btn  btn-primary", //按钮样式
        //         previewFileIcon: "<i class='glyphicon  glyphicon-king'></i>",
        //     });
        // }
        // $("#upload_btn").on("fileuploaded", function (event, data, previewId, index) {
        //     var data = data.response.lstOrderImport;
        //     if (data == undefined) {
        //         toastr.error('文件格式类型不正确');
        //         return;
        //     }
        // });
        function initFunc() {
            $(".input").val("");
            iniGridInfo();
            iniButtonAction();
            inModelGrid();// 初始化dailog中的grids
            otherActionBind();
            //初始化fileinput控件
            //initFileInput("upload_btn", "");
        }
        initFunc();
        //表格自适应
        function resizeFnc(){
            var offsetBottom, grdDivWidth,offsetBottom1, grdDivWidth1,offsetBottom2,
            //1
            grdDivWidth = $("#reasonListDiv").width();
            offsetBottom =  $("#reasonListDiv").offset().top;
            $("#reasonListDiv").height(offsetBottom*0.99);
            $("#reasonListGrd").setGridWidth(grdDivWidth*0.97);
            $("#reasonListGrd").setGridHeight(offsetBottom*0.80-51);
            //2
            grdDivWidth1 = $("#glassListDiv").width();
            offsetBottom1 = $(window).height() - $("#glassListDiv").offset().top;
            $("#glassListDiv").height(offsetBottom1*0.99);
            $("#glassListGrd").setGridWidth(grdDivWidth1*0.97);
            $("#glassListGrd").setGridHeight(offsetBottom1*0.95-51);
            // //3
            // grdDivWidth2 = $("#table3Div").width();
            // offsetBottom2 =$(window).height() -  $("#modalReasonListDiv").offset().top;
            //
            // $("#modalReasonListDiv").width(grdDivWidth2*0.98);
            // $("#modalReasonListDiv").height(offsetBottom2*0.99);
            // $("#modalReasonListGrd").setGridWidth(grdDivWidth2*0.60);
            // $("#modalReasonListGrd").setGridHeight(offsetBottom2*0.95-200);
            // //4
            // grdDivWidth3 = $("#table4Div").width();
            // offsetBottom3 =  $("#reModalReasonListDiv").offset().top;
            //
            // $("#reModalReasonListDiv").width(grdDivWidth3*0.98);
            // $("#reModalReasonListDiv").height(offsetBottom3*0.99);
            // $("#reModalReasonListGrd").setGridWidth(grdDivWidth3*0.97);
            // $("#reModalReasonListGrd").setGridHeight(offsetBottom3*0.95-51);
        };
        resizeFnc();
        $(window).resize(function() {
            resizeFnc();
        });

    })